/**
 * 
 */
package org.karthik.ecomm.main;

import org.hibernate.Session;
import org.karthik.ecomm.dto.Customer;
import org.karthik.ecomm.dto.Product;
import org.karthik.ecomm.util.ECommSessionFactory;

/**
 * @author Uday Shankar This is for collections with hibernate
 */
public class CustomerMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {


		Customer customer = new Customer();

		Product product1 = new Product();
		Product product2 = new Product();

		// Product 1
		product1.setProductId(1531);
		product1.setProductName("Surf Excel");
		product1.setProductPrice(98.00);

		// Product 2
		product2.setProductId(1789);
		product2.setProductName("Axe Deodarant");
		product2.setProductPrice(150.00);

		customer.setCustomerName("Manash Kalita");

		// Add the products to the List of products
		customer.getListOfProducts().add(product1);
		customer.getListOfProducts().add(product2);
		
		Session session = ECommSessionFactory.getInstance().getFactory().openSession();
		session.beginTransaction();

		session.save(customer);
		session.getTransaction().commit();
		session.close();

		customer = null;

		session = ECommSessionFactory.getInstance().getFactory().openSession();
		
		// if fetchType.EAGER collection wiill be pulled from DB here itself
		customer = (Customer) session.get(Customer.class, 1);
		
		// close the session to NOT provide a session for proxy object initialization
		session.close();
		
		//try to get the Collection object
		System.out.println(customer.getListOfProducts().size());

	}

}
